package com.atguigu.lease.web.admin.controller.apartment;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.web.admin.dto.ApartmentInfoSearchDto;
import com.atguigu.lease.web.admin.dto.ApartmentInfoSubmitDto;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.ApartmentInfoVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/apartment")
@Tag(name = "公寓管理模块")
public class ApartmentInfoController {
    @Resource
    ApartmentInfoService apartmentInfoService;

    //listInfoByDistrictId?id=110114
    @Operation(summary = "根据区县id查询公寓列表")
    @GetMapping("listInfoByDistrictId")
    //pojo入参 接收请求参数
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam("id") Long districtId){
        return Result.ok(apartmentInfoService.list(Wrappers.lambdaQuery(ApartmentInfo.class)
                .eq(ApartmentInfo::getDistrictId , districtId)));
    }
    //updateReleaseStatusById?id=1829774773517668354&status=1
    @Operation(summary = "更新公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam("id")Long id ,
                                          @RequestParam("status")Integer status){
        return Result.build(apartmentInfoService.update(Wrappers.lambdaUpdate(ApartmentInfo.class)
                .eq(ApartmentInfo::getId , id)
                .set(ApartmentInfo::getIsRelease , status)
                .set(ApartmentInfo::getUpdateTime, new Date())));
    }

    @Operation(summary = "条件分页查询公寓列表")
    @GetMapping("pageItem") //pageItem?current=1&size=3
    //pojo入参 接收请求参数
    public Result<Page<ApartmentInfoVo>> pageItem(ApartmentInfoSearchDto searchDto){
        return Result.ok(apartmentInfoService.pageItem(searchDto));
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增或更新公寓信息")
    public Result saveOrUpdate(@RequestBody ApartmentInfoSubmitDto submitDto){
        return Result.build(apartmentInfoService.saveOrUpdateApartmentInfo(submitDto));
    }
    //removeById?id=1829774773517668354
    @DeleteMapping("/removeById")
    @Operation(summary = "删除公寓信息")
    public Result removeById(@RequestParam Long id){
        return Result.build(apartmentInfoService.removeApartmetInfoById(id));
    }

    @Operation(summary = "查询公寓详情")
    @GetMapping("getDetailById") //pageItem?current=1&size=3
    //pojo入参 接收请求参数
    public Result<ApartmentDetailVo> getDetailById(@RequestParam("id") Long id){
        return Result.ok(apartmentInfoService.getDetailById(id));
    }
}
